package com.fyy.sentinel.controller;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @Author fyy
 * @Description 测试Sentinel流量控制
 * @Date 2021/7/8 10:49
 */
@RestController
public class TestController {

    @RequestMapping(value = "/test")
    public String test() {
        //SphU.entry 启用限流
        try (Entry Entry = SphU.entry("Test")) {
            return "你好，Sentinel";
        } catch (Exception e) {
            return "系统繁忙！";
        }
    }


    //定义限流规则
//    @PostConstruct
//    public void initFlowRules(){
//        //创建集合存放限流规则
//        List<FlowRule> rules = new ArrayList<>();
//        //创建限流规则
//        FlowRule rule = new FlowRule();
//        rule.setResource("Test"); //定义资源
//        rule.setGrade(RuleConstant.FLOW_GRADE_QPS); //定义限流规则类型
//        rule.setCount(1); //每秒可通过的请求个数
//        //存入集合
//        rules.add(rule);
//        //Sentinel加载集合
//        FlowRuleManager.loadRules(rules);
//    }

}
